package com.example.test.多线程;

/**
 * @author 晓果冻
 * @version 1.0
 * @date 2022/5/27 10:00
 */
public class RunnableDemo implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"我是Runnable子线程");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"Runnable子线程阻塞完毕");
    }

    public static void main(String[] args) {
        //启动线程
        System.out.println(Thread.currentThread().getName());
        new Thread(new RunnableDemo()).start();
        new Thread(new RunnableDemo()).start();
        System.out.println("主线程执行完毕");

        //匿名方式
        new Thread(new Runnable(){

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"我是匿名方式子线程");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"匿名方式子线程阻塞完毕");
            }
        }).start();

        //java8 lambda方式
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"我是lambda方式子线程");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"lambda方式子线程阻塞完毕");
        }).start();
    }
}
